package com.juc.threadPoolExecutor.threadPoolExecutor15.discardOldestPolicy;

import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Run {

	/**
	 * DiscardOldestPolicy:当任务添加到线程池中被拒绝时，线程池会放弃等待队列中
     * 最旧的未处理任务，然后将被拒绝的任务添加到等待队列中
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) throws InterruptedException {
		ArrayBlockingQueue queue = new ArrayBlockingQueue<>(2);
		ThreadPoolExecutor executor =
				new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.DiscardOldestPolicy());
		for (int i = 0; i < 5; i++) {
			MyRunnable myRunnable =new MyRunnable("Runnable" + (i + 1));
			executor.execute(myRunnable);
		}
		Thread.sleep(50);
		Iterator iterator = queue.iterator();
		while (iterator.hasNext()) {
			Object object =  iterator.next();
			System.out.println(((MyRunnable) object).getUsername());
		}
		executor.execute(new MyRunnable("Runnable6"));
		executor.execute(new MyRunnable("Runnable7"));
		iterator = queue.iterator();
		while (iterator.hasNext()) {
			Object object =  iterator.next();
			System.out.println(((MyRunnable) object).getUsername());
		}
		
	}
}
